﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace File
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "文本文件|*.txt";
            openDlg.Multiselect = true;
            openDlg.InitialDirectory = Application.StartupPath + @"\";
            openDlg.Title = "请选择文件";
            if(openDlg.ShowDialog() == DialogResult.OK)
            {
                string[] lines = System.IO.File.ReadAllLines(openDlg.FileName);
                foreach(string line in lines)
                {
                    textBox1.AppendText(line + Environment.NewLine);
                }
                label1.Text = openDlg.FileName;

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Filter = "文本文件|*.txt";
            saveDlg.InitialDirectory = Application.StartupPath + @"\";

            if(saveDlg.ShowDialog() == DialogResult.OK && saveDlg.FileName != null)
            {
                StreamWriter sw = new StreamWriter(saveDlg.FileName, true);
                sw.Write(textBox1.Text);
                sw.Flush();
                sw.Close();
                Application.Exit();
            }
        }
    }
}
